package com.sctu.EMSApp;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.*;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.adapter.PlayAmuseMoreListAdapter;
import com.sctu.bean.PlayAmuseMoreList;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.List;

/**
 * Created by zhengsenwen on 2015/12/17.
 */
public class PlayAmuseMoreListActivity extends BaseActivity {

    private ListView listView;
    //头部的title
    private TextView headtitleitext;
    private ImageView returnfragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.amuse_category_more);
        initViews();
    }

    private void initViews() {
        listView = (ListView) findViewById(R.id.amuse_more_list);

        returnfragment = (ImageView) findViewById(R.id.amuse_category_more_return_img);
        returnfragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PlayAmuseMoreListActivity.this.finish();
            }
        });
        String uuid = getIntent().getStringExtra("uuid_amuse");
        GetVolleyHttp.getInstance(this).getStringGETJsonData(UrlConfig.URL + "/category/" + uuid + "/articles", listener, errorListener);
    }

    Response.Listener<String> listener = new Response.Listener<String>() {
        @Override
        public void onResponse(String json) {
            Log.e("json", json);

            PlayAmuseMoreList playAmuseMoreList = JSON.parseObject(json, PlayAmuseMoreList.class);
            final List<PlayAmuseMoreList.Results> results = playAmuseMoreList.getResults();
            listView.setAdapter(new PlayAmuseMoreListAdapter(PlayAmuseMoreListActivity.this, results));

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent();
                    intent.setClass(PlayAmuseMoreListActivity.this, PlayAmuseOneDetailsActivity.class);
                    intent.putExtra("id_amuse", results.get(position).getId());
                    startActivity(intent);
                }
            });

            listView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_DOWN)
                    {
                        v.setBackgroundResource(R.color.item_touch_bac);
                    }
                    else if (event.getAction() == MotionEvent.ACTION_UP)
                    {v.setBackgroundResource(R.color.item_touch_bac);
                    }
                    return false;
                }
            });

            headtitleitext = (TextView) findViewById(R.id.amuse_category_more_title_txt);
            headtitleitext.setText(results.get(0).getCategory());


        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(PlayAmuseMoreListActivity.this, "网络出了些小问题", Toast.LENGTH_LONG)
                    .show();
        }
    };

}
